package com.orifine.utils;


public class ClassUtils {
	/**
	 * 例如：userMapper -> user_mapper
	 * XMLFile -> xmlfile
	 * @param className
	 * @return
	 */
	public static String convertClassNameToDatabaseName(String className) {
		StringBuffer newName = new StringBuffer();

		boolean isLower = false;
		for (int i = 0; i < className.length(); i++) {
			int code = className.codePointAt(i);
			if (code >= 65 && code <= 90) {
				if (isLower) {
					newName.append("_");
				}
				newName.append((char) (code + 32));
				isLower = false;
			} else if (code >= 97 && code <= 122) {
				isLower = true;
				newName.append((char) code);
			} else {
				isLower = false;
				newName.append((char) code);
			}
		}

		return newName.toString();
	}
	
	/**
	 * 例如：infoId -> id
	 * @param prefix
	 * @param prefixName
	 * @return
	 */
	public static String convertPrefixNameToSimpleName(String prefix, String prefixName){
		if(!prefixName.startsWith(prefix)){
			return prefixName;
		}
		
		return getUpperName(prefixName.substring(prefix.length()));
	}
	
	/**
	 * 例如：user -> User
	 * @param name
	 * @return
	 */
	public static String getUpperName(String name){
		StringBuffer newName = new StringBuffer(name);
		int code = newName.codePointAt(0);
		if(code >= 97 && code <= 122){
			newName.setCharAt(0, (char) (code-32));
		}
		return newName.toString();
	}
	
	/**
	 * 例如：User -> user
	 * @param name
	 * @return
	 */
	public static String getLowerName(String name){
		StringBuffer newName = new StringBuffer(name);
		int code = newName.codePointAt(0);
		if(code >= 65 && code <= 90){
			newName.setCharAt(0, (char) (code+32));
		}
		return newName.toString();
	}
}
